//
//  ItemVC3.m
//  喜马拉雅FM
//
//  Created by dllo on 16/1/22.
//  Copyright © 2016年 lanou.com. All rights reserved.
//

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

#import "ItemVC3.h"
#import "ModelForSecendVC.h"
#import "UIImageView+WebCache.h"
#import <AVFoundation/AVFoundation.h>

#import <MJRefresh.h>
#import "Player.h"
#import "PlayerSingle.h"

@interface ItemVC3 ()

@property (nonatomic, retain) NSMutableArray *arrForUrl;

@end

@implementation ItemVC3

+ (instancetype)shareItemVC3 {

    // 在静态区,只初始化一次 创建ItemVC3对象.
    static ItemVC3 *itemThree = nil;
    static dispatch_once_t predicate;
    
   dispatch_once(&predicate, ^{
            itemThree = [[ItemVC3 alloc]init];
   });

    return itemThree;
}

#pragma mark ViewController的生命周期.

- (void)viewWillAppear:(BOOL)animated {

}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    
    NSString *urlStr = self.musicUrlString;
    urlStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSURL *url = [NSURL URLWithString:urlStr];
    
    self.arrForUrl = [NSMutableArray array];
    for (ModelForSecendVC *model in self.arrForModel) {
        
        [self.arrForUrl addObject:model.playUrl64];
    }
    
    [[PlayerSingle sharePlayer]playWithUrl:url showView:self.view];
    [self getValue];

    // 通知中心记得移除,代理方法记得置空.dealloc.
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(handleBack:) name:@"返回" object:nil];
    [center addObserver:self selector:@selector(handleXiaYiQu:) name:@"下一曲" object:nil];
    [center addObserver:self selector:@selector(handleShangYiQu:) name:@"上一曲" object:nil];
}

// 给播放界面的赋值.
- (void)getValue {
    
    [[PlayerSingle sharePlayer].imageBackGroundView sd_setImageWithURL:[NSURL URLWithString:self.model.coverLarge]];
    [[PlayerSingle sharePlayer].imageCoverLarge sd_setImageWithURL:[NSURL URLWithString:self.model.coverLarge]];
    [PlayerSingle sharePlayer].labelForTitle.text = self.model.title;
}


- (void)handleBack:(NSNotificationCenter *)center {

    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)handleXiaYiQu:(NSNotificationCenter *)center {

    NSLog(@"下一曲");
    
    if (self.index < self.arrForModel.count - 1) {
        
    [[PlayerSingle sharePlayer].player replaceCurrentItemWithPlayerItem:[AVPlayerItem playerItemWithURL:[NSURL URLWithString:self.arrForUrl[self.index + 1]]]];
    ModelForSecendVC *model = self.arrForModel[self.index + 1];
    
    [[PlayerSingle sharePlayer].imageBackGroundView sd_setImageWithURL:[NSURL URLWithString:model.coverLarge]];
    [[PlayerSingle sharePlayer].imageCoverLarge sd_setImageWithURL:[NSURL URLWithString:model.coverLarge]];
    [PlayerSingle sharePlayer].labelForTitle.text = model.title;
    // VC的内容视图更新.
//    [self reloadInputViews];
    self.index++;
        
    }else {

        self.index = 0;
        [[PlayerSingle sharePlayer].player replaceCurrentItemWithPlayerItem:[AVPlayerItem playerItemWithURL:[NSURL URLWithString:self.arrForUrl[0]]]];
        ModelForSecendVC *model = self.arrForModel[0];
        
        [[PlayerSingle sharePlayer].imageBackGroundView sd_setImageWithURL:[NSURL URLWithString:model.coverLarge]];
        [[PlayerSingle sharePlayer].imageCoverLarge sd_setImageWithURL:[NSURL URLWithString:model.coverLarge]];
        [PlayerSingle sharePlayer].labelForTitle.text = model.title;
    }
}


- (void)handleShangYiQu:(NSNotificationCenter *)center {

    NSLog(@"上一曲");
    if (self.index > 0) {
        
    [[PlayerSingle sharePlayer].player replaceCurrentItemWithPlayerItem:[AVPlayerItem playerItemWithURL:[NSURL URLWithString:self.arrForUrl[self.index - 1]]]];
    ModelForSecendVC *model = self.arrForModel[self.index - 1];
    
    [[PlayerSingle sharePlayer].imageBackGroundView sd_setImageWithURL:[NSURL URLWithString:model.coverLarge]];
    [[PlayerSingle sharePlayer].imageCoverLarge sd_setImageWithURL:[NSURL URLWithString:model.coverLarge]];
    [PlayerSingle sharePlayer].labelForTitle.text = model.title;
    // VC的内容视图更新.
//    [self reloadInputViews];
    self.index--;
    }else {
    
        self.index = self.arrForModel.count - 1;
        [[PlayerSingle sharePlayer].player replaceCurrentItemWithPlayerItem:[AVPlayerItem playerItemWithURL:[NSURL URLWithString:self.arrForUrl[self.arrForModel.count - 1]]]];
        ModelForSecendVC *model = self.arrForModel[self.arrForModel.count - 1];
        
        [[PlayerSingle sharePlayer].imageBackGroundView sd_setImageWithURL:[NSURL URLWithString:model.coverLarge]];
        [[PlayerSingle sharePlayer].imageCoverLarge sd_setImageWithURL:[NSURL URLWithString:model.coverLarge]];
        [PlayerSingle sharePlayer].labelForTitle.text = model.title;
        // VC的内容视图更新.
        [self reloadInputViews];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}



@end
